# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Renato Silva <br.renatosilva@gmail.com>

_realname=libxml2
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.9.4
pkgrel=2
arch=('any')
pkgdesc="XML parsing library, version 2 (mingw-w64)"
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-gettext"
         "${MINGW_PACKAGE_PREFIX}-xz"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc" "${MINGW_PACKAGE_PREFIX}-pkg-config" "${MINGW_PACKAGE_PREFIX}-python2")
options=('strip' 'staticlibs')
license=(MIT)
url="http://www.xmlsoft.org/"
install=${_realname}-${CARCH}.install
source=("http://xmlsoft.org/sources/${_realname}-${pkgver}.tar.gz"
        "mingw32-libxml2-static-build-compile-fix.patch"
        0004-detect-ws2tcpip.mingw.patch
        0005-fix-char-cast-warning.mingw.patch
        0006-fix-unused-var-warning.mingw.patch
        0007-fix-stat-redefinition.mingw.patch
        0008-include-winsock2.h-before-windows.h.mingw.patch
        0009-use-wstat-appropriate-for-mingw-w64.mingw.patch
        0010-mingw-w64-defines-lots-of-errnos.mingw.patch
        0011-more-winsock-inclusion-order-fixes.mingw.patch
        0012-socklen-is-signed-on-mingw-w64.mingw.patch
        0013-fix-field-type-signedness.all.patch
        0015-fix-unused-parameters-warning.all.patch
        0016-WARNING-to-be-fixed.all.patch
        0017-fix-const-warning.all.patch
        0018-function-declaration-isnt-prototype.all.patch
        0019-unused-flags.all.patch
        0020-fix-size_t-format-specifier.all.patch
        0023-fix-sitedir-detection.mingw.patch
        0024-shrext-pyd.mingw.patch
        0025-mingw-python-dont-remove-dot.patch)
sha256sums=('ffb911191e509b966deb55de705387f14156e1a56b21824357cdf0053233633c'
            '0f86ded5d487ae7f38e1f039085c078d978af8d7aad6e05d5a4028d645d2115a'
            '6ae1cee98b4ac9944a26a0bac75db48ae718d755dad3d37638da9b33a65c5789'
            'd954213b42b5c9f2ccf9211420e40a223fc1cea798b306437c010b25f6c36cc5'
            '148da5aabe2c1213ba609e893557e455d79ac6490da276f2d0b53d2d4aa6274b'
            '8238eeba1da80cd965feaf4d372995d0ae4572fc5cf45e3d739a659b4dce150b'
            'f8457f2ad7ab39110f79ff087ebc058a3ecfd70509b80aa3668d837738849ab1'
            'abe81bbc11421fec352e4432b730ffd60423681e31e6a65480d5632a457326ff'
            '8e3f85732dc3d1d895028c484b0642c4ed10607fa66a461581f004845581f656'
            '8c53c7adb3b33ed95fc40ea4f5655b7f08026345edde32204a125b9fcf7aae2f'
            'e9241c7a9ed42594dc6f17fabd2c0de3f07eb4db5f774ee18deba27bfaeb91d4'
            '3a4d79594416021d6c101c364c1a631142e40827a00fe5e88dba7395c422d9e5'
            'd4a30a07ce44f8c55f1ba31b86336441ba02be7621e669360b26fad2269d12db'
            '81811dc3a497025b1f7c8fe78b7510ab080be558a646c26b89b281a9348ad195'
            'd8f81b074420a5082a2f64fda613974ffc43a04867d521dccdda9b6ddeb3422b'
            '6e9491dcaba7e7605ddd66a152317beb3f76087f406c0fccb60a051ad24a1dd8'
            '3f025f34bf0c9b084905d24a7195315bccba45a617a02cd051cd5ee791fad098'
            '3ed050187cf40fc3c91009e60e3d9b58d9d0fbe6e089469306f8689ff62653a0'
            'e93902af20bee41ca7708280f30a1d087ab8c6f86d18939bdd56789cd1a73531'
            'bc182ef5108f0d317543e7d3ca309bbe2fc1faf832a49b3f56acc2795de2b626'
            '2d177b78b348be6466e545ef2df533c860983bf0811d1d14cdd04e1f3afef83f')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"

  patch -Np0 -i "${srcdir}"/mingw32-libxml2-static-build-compile-fix.patch

  patch -p1 -i ${srcdir}/0004-detect-ws2tcpip.mingw.patch
  patch -p1 -i ${srcdir}/0005-fix-char-cast-warning.mingw.patch
  patch -p1 -i ${srcdir}/0006-fix-unused-var-warning.mingw.patch
  patch -p1 -i ${srcdir}/0007-fix-stat-redefinition.mingw.patch
  patch -p1 -i ${srcdir}/0008-include-winsock2.h-before-windows.h.mingw.patch
  patch -p1 -i ${srcdir}/0009-use-wstat-appropriate-for-mingw-w64.mingw.patch
  patch -p1 -i ${srcdir}/0010-mingw-w64-defines-lots-of-errnos.mingw.patch
  patch -p1 -i ${srcdir}/0011-more-winsock-inclusion-order-fixes.mingw.patch
  patch -p1 -i ${srcdir}/0012-socklen-is-signed-on-mingw-w64.mingw.patch
  patch -p1 -i ${srcdir}/0013-fix-field-type-signedness.all.patch
  patch -p1 -i ${srcdir}/0015-fix-unused-parameters-warning.all.patch
  patch -p1 -i ${srcdir}/0016-WARNING-to-be-fixed.all.patch
  patch -p1 -i ${srcdir}/0017-fix-const-warning.all.patch
  patch -p1 -i ${srcdir}/0018-function-declaration-isnt-prototype.all.patch
  patch -p1 -i ${srcdir}/0019-unused-flags.all.patch
  patch -p1 -i ${srcdir}/0020-fix-size_t-format-specifier.all.patch
  patch -p1 -i ${srcdir}/0023-fix-sitedir-detection.mingw.patch
  patch -p1 -i ${srcdir}/0024-shrext-pyd.mingw.patch
  patch -p1 -i ${srcdir}/0025-mingw-python-dont-remove-dot.patch

  libtoolize --copy --force
  aclocal
  automake --add-missing
  autoconf
}

build() {

  # Static build
  mkdir -p ${srcdir}/build-static-${CARCH} && cd ${srcdir}/build-static-${CARCH}
  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --build="${MINGW_CHOST}" \
    --without-python \
    --with-modules \
    --enable-static \
    --disable-shared \
    --with-threads=win32 \
    CFLAGS="${CFLAGS} -DLIBXML_STATIC_FOR_DLL"
  make

  # Shared build
  mkdir -p ${srcdir}/build-shared-${CARCH} && cd ${srcdir}/build-shared-${CARCH}
  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --build="${MINGW_CHOST}" \
    --with-python=${MINGW_PREFIX}/bin/python2 \
    --with-modules \
    --disable-static \
    --enable-shared \
    --with-threads=win32

  # Hack for mingw python
  local PREFIX_DEPS=$(cygpath -am ${MINGW_PREFIX})
  find . -type f -name "Makefile" -exec sed -i "s|${PREFIX_DEPS}|${MINGW_PREFIX}|g" {} \;
  make
}

package() {
  # First install shared
  cd ${srcdir}/build-shared-${CARCH}

  make install DESTDIR="${pkgdir}"

  # then manually install static
  cd ${srcdir}/build-static-${CARCH}

  install -m 0644 .libs/libxml2.a "${pkgdir}${MINGW_PREFIX}"/lib/

  # License
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
  rm "${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}-${pkgver}/Copyright" # This is the same file as COPYING
}
